﻿using AutoMapper;
using lwzlz.Core;
using lwzlz.Core.Repository;
using System;
using System.Collections.Generic;
using System.Linq;

namespace lwzlz.Application.System
{
    public class PlaceService
    {
        private readonly IRepository<Place> _repository;

        public PlaceService()
        {
            _repository = new Repository<Place>();
        }

        #region 查询

        public Place Get(int id)
        {
            return _repository.Get(id);
        }

        //获得分页数据
        public IEnumerable<dynamic> GetDynamicPaged(int pageNumber, int rowsPerPage, string orderby, string sql, object param = null)
        {
            return _repository.GetDynamicPaged(pageNumber, rowsPerPage, orderby, sql, param);
        }

        public int RecordQueryCount(string sql, object param)
        {
            return _repository.RecordQueryCount(sql, param);
        }

        #endregion

        #region 编辑

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int? Add(Place model)
        {
            return _repository.Insert(model);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpDataSave(Place model)
        {
            return _repository.Update(model);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="IdList"></param>
        public void DeleteList(string IdList)
        {
            var dbtran = _repository.BeginTransaction();//开启事务
            try
            {
                foreach (string Id in IdList.Split(','))
                {
                    Place model = _repository.Get(Id, dbtran);
                    _repository.Delete(model, dbtran);
                }
                dbtran.Commit();//提交事务
            }
            catch (Exception)
            {
                dbtran.Rollback();//事务回滚
            }
            finally
            {
                dbtran.Dispose();//释放事务资源
            }
        }
        #endregion

        #region 获取地区select数据
        public IEnumerable<Place> GetSelectList(string pid)
        {
            return _repository.Query<Place>("select Code,Name from Sys_Place where ParentID=@Pid order by ID", new { @Pid = pid });
        }
        #endregion

        #region 通过code获得地区名
        public string GetPlaceName(string code)
        {
            return _repository.QueryFirstOrDefault("select Name from Sys_Place where Code=@Code", new { @Code = code }).Name;
        }

        public List<Place> GetDeepList(int deep)
        {
            return _repository.Query<Place>("select Code,Name from Sys_Place where deep=@Deep order by ID", new { Deep = deep }).ToList();
        }
        #endregion

        #region 获得省
        public List<Place> GetDeep1()
        {
            var list = _repository.QueryDynamic(" select * from Sys_Place where Deep=1");
            return Mapper.Map<List<Place>>(list);
        }
        #endregion

        #region 获得市
        public List<Place> GetDeep2()
        {
            var list = _repository.QueryDynamic(" select * from Sys_Place where Deep=2");
            return Mapper.Map<List<Place>>(list);
        }
        #endregion

        #region 获得区
        public List<Place> GetDeep3()
        {
            var list = _repository.QueryDynamic(" select * from Sys_Place where Deep=3");
            return Mapper.Map<List<Place>>(list);
        }
        #endregion
    }
}
